<?php
	include('include/header.php');
	include("include/commands.php");

        //Check if we are coming to this page from an insert query.
        if ($_POST["fromInsert"] == "true") {
            
            //Instantiate connector.
            $connector = new CloudDBInterface();
            
            //Check if we need to add a new Employee_Type
            if ($_POST["newEType"] != "") {
                
                //Grab variables.
                $newEType = $_POST["newEType"];
                $newETypeDesc = $_POST["newETypeDesc"];
                
                //Create the query.
                $query =    "INSERT INTO Employee_Type (name, description)
                            VALUES (\"$newEType\", \"$newETypeDesc\")";
                
                //Insert the tuple.
                $connector->insertQuery($query);
            }

            //Add the new Employee now.
            //Get variables.
            $fName = $_POST["fName"];
            $lName = $_POST["lName"];
            $pNumber = $_POST["pNumber"];
            $address = $_POST["address"];
            $hourlyRate = $_POST["hourlyRate"];

            //Get the appropriate employee type.
            if ($_POST["newEType"] != "") {
                
                $eType = $_POST["newEType"];
            } else {
                
                $eType = $_POST["employeeType"];
            }
            
            //Get the right ID.
            $query =    "SELECT id
                        FROM Employee_Type
                        WHERE name = \"$eType\"";
            
            //Query and retrieve result.
            $resultArray = $connector->selectQuery($query);
            $eTypeId = $resultArray[0]["id"];

            //Create the query.
            $query =    "INSERT INTO Employee (Fname, LName, HireDate, PhoneNumber, Address, HourlyRate, EmployeeTypeId)
                        VALUES (\"$fName\", \"$lName\", CURDATE(), $pNumber, \"$address\", $hourlyRate, $eTypeId)";

            //Insert the tuple.
            $connector->insertQuery($query);            
        }

	if(isset($_GET['sortBy'])) {

		$start = null;
		$end = null;
		$id = null;

		if(isset($_GET['start'])){
			$start = $_GET['start'];
		}
		if(isset($_GET['end'])){
			$end = $_GET['end'];
		}
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}

		$employees = getEmployeesSorted($_GET['sortBy'],$start,$end,$id);
	}
	else {
		$employees = getEmployees();
	}
?>

	<div class="body">
	

		<h1> Employee Directory </h1>
		<hr style="length:200px;width:2px" />

		<div class="border">
			<form>
			<p>
			Date From <input type="text" name="start"/>
			Date To <input type="text" name="end"/>(yyyy-mm-dd)
			<br/>
			Employee ID: <input type="text" name="id"/>
			<br/>		
			Sort By:
				<select name="sortBy">
					<option>NONE</option>
					<option>Seniority</option>
					<option>Salary</option>
				</select>
			<input type="submit" value="Filter"/>
			</p>
			</form>
		</div>
                <br/>
                <div class="border">
                    <form id="newEmployeeForm" action="employee.php" method="post" onsubmit="return validateEmployeeInsert();">
                        <h4>Add a New Employee</h4>
                        <p>
                            First Name* <input type="text" name="fName" id="fName"/><br/>
                            Last Name* <input type="text" name="lName" id="lName"/><br/>
                            Phone Number* <input type="text" name="pNumber" id="pNumber"/><br/>
                            Address* <input type="text" name="address" id="address"/><br/>
                            Hourly Rate* <input type="text" name="hourlyRate" id="hourlyRate"/><br/>
                            EmployeeType <?php employeeTypes(); ?><br/>
                            or Add a New Employee Type <input type="text" name="newEType" id="newEType"/><br/>
                            New Employee Type Description <input type="text" name="newETypeDesc" id="newETypeDesc"/><br/>
                            <input type="hidden" name="fromInsert" id="fromInsert" value="true"/>
                            <input type="submit" value="Submit"/>
                        </p>
                    </form>
                </div>
	<br/>
		<?php 
			
			if(sizeof($employees) > 0){
					generateEmployeeTable($employees);
				}
				else{
					echo "<p> No Employee(s) Found. </p>";
				}
				
				function generateEmployeeTable($employees) {

					echo "<table>";
					echo "<tr>";
					echo "<th>Employee Id</th>";
					echo "<th>First Name</th>";				
					echo "<th>Last Name</th>";
					echo "<th>Hire Date</th>";
					echo "<th>Phone Number</th>";
					echo "<th>Address</th>";
					echo "<th>Hourly Rate</th>";
					echo "<th>Employee Type</th>";
					echo "</tr>";
					generateEmployeeTableBody($employees);
					echo "</table>";
				}
		?>
	</div>


<?php 
	include('include/footer.php'); 
?>